package com.example.demo_2;
import com.example.demo_2.StuDao;
import com.example.demo_2.Stu;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginService {

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 验证用户登录信息
     * @param username 用户名
     * @param password 密码
     * @return 如果登录成功返回true，否则返回false
     */
    public boolean login(String username, String password) {
        try (SqlSession session = sqlSessionFactory.openSession()) {
            StuDao stuDao = session.getMapper(StuDao.class);
            Stu stu = stuDao.getUserByName(username);
            if (stu != null && stu.getPassword().equals(password)) {
                return true;
            }
        }
        return false;
    }
}
//public interface LoginService {
//     boolean login(String username, String password);
//}
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.stereotype.Service;

//    boolean stulogin(String username, String password);
//@Service
//public abstract class LoginService {
//
//    private static final Logger log = LoggerFactory.getLogger(LoginService.class);
//
//    // 模拟用户数据
//    private static final String VALID_USERNAME = "zth";
//    private static final String VALID_PASSWORD = "zth";
//
//    public boolean login(String username, String password) {
//        log.info("尝试登录: 用户名={}, 密码={}", username, password);
//        if (VALID_USERNAME.equals(username) && VALID_PASSWORD.equals(password)) {
//            log.info("登录成功: 用户名={}", username);
//            return true;
//        } else {
//            log.warn("登录失败: 用户名={}, 密码={}", username, password);
//            return false;
//        }
//    }
//
//    public abstract boolean stulogin(String username, String password);
//}

